/* 
 *
 * Persistence4J - Simple library for data persistence using java
 * Copyright (c) 2010, Avdhesh yadav.
 * http://www.avdheshyadav.com
 * Contact: avdhesh.yadav@gmail.com
 *
 * This library is free software; you can redistribute it and/or modify it under the terms
 * of the GNU Lesser General Public License as published by the Free Software Foundation,
 * either version 3 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
 * without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
 * See the GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License along with this
 * library; if not, write to the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
 * Boston, MA 02111-1307, USA.
 *
 */

package com.avdy.p4j.jdbc.dbms.connfactory.xa;

import java.sql.Connection;
import java.sql.SQLException;

import javax.sql.XADataSource;

import org.apache.log4j.Logger;
import org.enhydra.jdbc.pool.StandardXAPoolDataSource;
import org.enhydra.jdbc.standard.StandardXADataSource;
import org.objectweb.transaction.jta.TMService;

import com.avdy.p4j.jdbc.dbms.DataSourceAttr;

/**
 * 
 * @author Avdhesh Yadav
 */
class JotmXAProviderImpl extends AbsXaProviderImpl implements XAProvider
{
	private static Logger logger = Logger.getLogger(JotmXAProviderImpl.class.getName());
	//
	private StandardXAPoolDataSource mPooledXADS;
	//
	protected boolean mPoolStarted = true;
	
	
	/**
	 * 
	 * @param dsAttr DataSourceAttr
	 * 
	 * @throws Exception
	 */
	public JotmXAProviderImpl(String database, DataSourceAttr dsAttr) throws Exception
	{
		super(database, dsAttr);
		mPooledXADS = new StandardXAPoolDataSource();
		initilizeDatasource();
	}
	
	
	/**
	 * 
	 * @param driverName String
	 * @param url String
	 * @param user String
	 * @param password String
	 * 
	 * @throws Exception
	 */
	private void initilizeDatasource() throws Exception
	{	
		// initilize the xa datasource
		XADataSource xaDataSource =  new StandardXADataSource();
		((StandardXADataSource) xaDataSource).setDriverName(mDsAttr.getDriverClassName());
		((StandardXADataSource) xaDataSource).setUrl(mDsAttr.getUrlString(mDatabase));
		((StandardXADataSource) xaDataSource).setTransactionIsolation(Connection.TRANSACTION_READ_COMMITTED);
		
		// now initialize the xa connection pool
		mPooledXADS.setUser(mDsAttr.getUserName());
		mPooledXADS.setPassword(mDsAttr.getPassword());
		mPooledXADS.setMinSize(mDsAttr.getMinPoolSize());
		mPooledXADS.setMaxSize(mDsAttr.getMaxPoolSize());
		
		TMService m_Jotm = JOTMInstance.getTMService();
		
		mPooledXADS.setTransactionManager(m_Jotm.getTransactionManager());
		mPooledXADS.setGC(true);
		mPooledXADS.setDataSource(xaDataSource);
		mPooledXADS.setCheckLevelObject(2);
		mPooledXADS.setJdbcTestStmt(mTestQuery);
		
		// get User Transaction
		mUserTransaction = m_Jotm.getUserTransaction();
		mUserTransaction.setTransactionTimeout(60);
	}	
	 
	 
	/**
	 * 
	 *  @return DataSource
	 */
	public XADataSource getXaDataSource()throws SQLException
	{
		return mPooledXADS.getDataSource();
	}
	
	
	/**
	 * 
	 * @return Connection
	 */
	public Connection getConnection()throws SQLException
	{
		if(mPoolStarted)
		{
			return mPooledXADS.getConnection();
		}
		else
		{
			String errMsg = "Pool is not started .Please start pool before getting connection from the datasource";
			logger.error(errMsg);
			throw new SQLException(errMsg);
		}
	}
	
	
	/**
	 * 
	 */
	public void stop()
	{
		mPooledXADS.stopPool();
		mPoolStarted = false;
	}
}